using UnityEngine;
using QFramework;
using UnityEngine.Tilemaps;
using UnityEngine.SceneManagement;
using System.Linq;
using Unity.VisualScripting;

namespace ProjectindieFarm
{
	
	public partial class player : ViewController
	{
		public GridLayout Grid;
		public Tilemap tilemap;
		public TileBase mPen;

		private void Awake()
		{
			Global.Player = this;
		}
		void Start()
		{
			// Code HereG
			Global.Days.Register(day=>
			{
				var cellPosition = Grid.WorldToCell(transform.position);

				var grid = FindObjectOfType<Gridconller>().ShowGrid;
				//-----------种子生长
				PlantController.Instance.Plants.ForEach((x,y,plant)=>
				{			
					if(plant)
					{
						if(plant.State == PlantStates.Seed)
						{
							if(grid[x,y].Watered)
							{
								plant.SetState(PlantStates.Small,x,y);
							}
						}
						else if (plant.State == PlantStates.Small)
						{
							if(grid[x,y].Watered)
							{
								plant.SetState(PlantStates.Ripe,x,y);
							}
						}
					}
				});
				//----------水
				var water = SceneManager.GetActiveScene()
					.GetRootGameObjects()
					.Where(gameObj => gameObj.name.StartsWith("water"));
				foreach (var w in water)
				{
					var c = Grid.WorldToCell(w.Position());
					grid[c.x,c.y].Watered = false;
					w.DestroySelf();
				}
			});
		}
		private void OnGUI()
		{
			IMGUIHelper.SetDesignResolution(640,360);
			GUILayout.Space(5);
			GUILayout.BeginHorizontal();
			GUILayout.Space(10);
			GUILayout.Label("天数:"+Global.Days.Value);
			GUILayout.EndHorizontal();

			GUILayout.Space(5);
			GUILayout.BeginHorizontal();
			GUILayout.Space(5);
			GUILayout.Label("果子:"+Global.FruitCount.Value);
			GUILayout.EndHorizontal();

			GUILayout.Space(5);
			GUILayout.BeginHorizontal();
			GUILayout.Space(5);
			GUILayout.Label("下一天：F");
			GUILayout.EndHorizontal();

			GUILayout.Space(5);
			GUILayout.BeginHorizontal();
			GUILayout.Space(5);
			GUILayout.Label("浇水：E");
			GUILayout.EndHorizontal();

			GUILayout.Space(5);
			GUILayout.BeginHorizontal();
			GUILayout.Space(5);
			GUILayout.Label($"当前工具：{Constant.DisplayName(Global.CurrentTool.Value)}");
			GUILayout.EndHorizontal();
		}
		private void Update()
		{
			if(Input.GetKeyDown(KeyCode.F))//键盘F转到明天
			{
				Global.Days.Value++;
			}
			var cellPosition = Grid.WorldToCell(transform.position);
			TelSecletConoller.Instance.Position(cellPosition);
			TelSecletConoller.Instance.Show();

			if(Input.GetMouseButtonDown(0))//鼠标左键种田
			{
				var grid = FindObjectOfType<Gridconller>().ShowGrid;//耕地
				if(grid[cellPosition.x,cellPosition.y] == null &&	Global.CurrentTool.Value == Constant.TOOL_SHOVEL)
				{
					tilemap.SetTile(cellPosition,mPen);  
					grid[cellPosition.x,cellPosition.y] = new SoilData();
				}

				else if(grid[cellPosition.x,cellPosition.y] !=null && !grid[cellPosition.x,cellPosition.y].HasPlant && Global.CurrentTool.Value == Constant.TOOL_SEED){//放种子
					var plantGameObject = ResConcoller.Instance.PlanPrefab.Instantiate().Position(cellPosition);
					PlantController.Instance.Plants[cellPosition.x,cellPosition.y]= plantGameObject.GetComponent<Plant>();
					grid[cellPosition.x,cellPosition.y].HasPlant = true;
				}
				else if(grid[cellPosition.x,cellPosition.y] != null && grid[cellPosition.x,cellPosition.y].PlantStates == PlantStates.Ripe && Global.CurrentTool.Value == Constant.TOOL_HAND)//枯萎
				{
					PlantController.Instance.Plants[cellPosition.x,cellPosition.y].SetState(PlantStates.Old,cellPosition.x,cellPosition.y);
					Global.FruitCount.Value+=1;

				}
				else if(grid[cellPosition.x,cellPosition.y] != null && grid[cellPosition.x,cellPosition.y].PlantStates == PlantStates.Old && Global.CurrentTool.Value == Constant.TOOL_SHOVEL)//重新耕地
				{
					Destroy(PlantController.Instance.Plants[cellPosition.x,cellPosition.y].gameObject);
					grid[cellPosition.x,cellPosition.y].HasPlant = false;

				}
			}
			if(Input.GetKeyDown(KeyCode.E))//键盘E胶水
			{
				var grid = FindObjectOfType<Gridconller>().ShowGrid;

				if(grid[cellPosition.x,cellPosition.y] != null&&!grid[cellPosition.x,cellPosition.y].Watered && Global.CurrentTool.Value == Constant.TOOL_WATERING_SCAN)
				{

					ResConcoller.Instance.Water.Instantiate().Position(cellPosition);
					grid[cellPosition.x,cellPosition.y].Watered = true;

				}

			}
			if(Input.GetKeyDown(KeyCode.Return))//键盘通关
			{
				SceneManager.LoadScene("GamePass");
			}


		}
		private void OnDeselect()
		{
			Global.Player = null;
		}
	}
}
